# myapp/middleware.py
from django.http import JsonResponse
class LoginRequiredMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        self.whitelist = ['/gamesdemo1/', '/signup/', '/admin/']  # 放行的路径
    def __call__(self, request):
        path = request.path_info
        if not request.user.is_authenticated and path not in self.whitelist:
            return JsonResponse({'detail': '请先登录'}, status=401)
        return self.get_response(request)

